package com.xm.listener;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConfirmListener;
import com.xm.model.Msg;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.concurrent.atomic.AtomicInteger;

@Component
@RabbitListener(queues = "directQueueAckNew")
@Slf4j
public class AckReceiverNew {
    private final AtomicInteger number = new AtomicInteger();

    @RabbitHandler
    public void process(Msg msg, Message amqpMsg, Channel channel) throws IOException {
        long deliveryTag = amqpMsg.getMessageProperties().getDeliveryTag();
        int i = number.addAndGet(1);
        log.info("ackReceiverNew(process)消费者收到消息[{}]", i);
        channel.basicAck(deliveryTag, false);
    }

    @RabbitHandler
    public void process2(String msgStr, Message amqpMsg, Channel channel) throws IOException {
        long deliveryTag = amqpMsg.getMessageProperties().getDeliveryTag();
        int i = number.addAndGet(1);
        log.info("ackReceiverNew(process2)消费者收到消息[{}]，msg={}", i, msgStr);
        channel.basicAck(deliveryTag, false);
    }

    @RabbitHandler
    public void process3(LinkedHashMap<String, Object> msgHashMap, Message amqpMsg, Channel channel) throws IOException {
        long deliveryTag = amqpMsg.getMessageProperties().getDeliveryTag();
        int i = number.addAndGet(1);
        log.info("ackReceiverNew(process3)消费者收到消息[{}]，msg={}", i, msgHashMap);
        channel.basicAck(deliveryTag, false);
    }

}
